import {fetch, getLocaleDefault, parseSql, callApi} from '../util'
import {chartConfig, columnConfig, pieConfig} from './ConfigGraph'
const chart = {
  name: 'chart',
  initialState: {
    chartObj: []
  },
  sagas: {
    *cleanState(action, {update, put, call}){
      yield update({chartObj: []})
    },
    *fetchGraphSql(action, {update, put, call}) {

      yield  update({loading: true})
      const response = yield call(fetch, `/system/api/v1/modules/multilanguage/REPORT_MODULES/${action.modId}`)
      let chartsParam = eval(response.payload.filter(item=>item.LOCALE == getLocaleDefault())[0].CHARTS_DEF)
      let chartObj = []
      for (let i = 0; i < chartsParam.length; i++) {
        let chartItem = {}
        chartItem.id = i
        chartItem.type = chartsParam[i].type
        chartItem.isLoading = true
        chartItem.title = chartsParam[i].title.text
        chartItem.subtitle = chartsParam[i].subtitle.text
        chartItem.needDatetime = chartsParam[i].needDatetime
        chartItem.sql = chartsParam[i].querySql
        chartItem.needDateState = chartsParam[i].needDatetime == false ? false : true
        chartItem.prevReport = chartsParam[i].prevReport ? chartsParam[i].prevReport.id ? chartsParam[i].prevReport.id : null : null
        chartItem.nextReport = chartsParam[i].nextReport ? chartsParam[i].nextReport.id ? chartsParam[i].nextReport.id : null : null
        chartObj.push(chartItem)

      }

      yield update({chartObj: chartObj})
      for (let i = 0; i < chartsParam.length; i++) {
        if (chartsParam[i].needDatetime == false) {
          yield put({
            type: 'chart/fetchSql',
            sql: parseSql(chartsParam[i].querySql),
            index: i,
            chartObj: chartObj
          })
        }
      }


    },
    *fetchSql(action, {update, pull, call, select}){


      try {
        let sql = action.sql
        let res = yield call(fetch, '/system/api/v2/budget-combinations/reportData', {
          type: 'PUT',
          data: {sql: sql}
        })

        const {chartState} = yield select(state => ({
          chartState: state,
        }))

        let chartObj = chartState.chart.chartObj

        if (res.payload != null && res.payload.length > 0) {
          let scales = res.payload.map(item=>item.X_SCALE)
          let pieScales = res.payload.map(item=>Object.assign({}, {
            name: item.X_SCALE,
            y: item.Y_SCALE1 == null ? 0 : item.Y_SCALE1
          }))

          let scaleContainer = []

          let objLength = Object.keys(res.payload[0]).length

          for (let i = 1; i < objLength; i++) {
            let key = `Y_SCALE${i}`
            let scale = res.payload.map(item=> {
              return item[key] == null ? 0 : item[key]
            })
            scaleContainer.push({name: key, data: scale})
          }

          let filterChartObj = chartObj.map((item, index)=>
            index == action.index ? {
              ...item,
              scales: scales,
              pieScales: pieScales,
              scaleContainer: scaleContainer,
              isLoading: false,
              needDateState: false
            } : item
          )

          yield update({chartObj: filterChartObj})
        } else {
          yield update({
            chartObj: chartObj.map(item=>item.id == action.index ?
            {...item, isLoading: false} : item)
          })
        }
      } catch (e) {
       //网络异常处理
        const {chartState} = yield select(state => ({
          chartState: state,
        }))

        let chartObj = chartState.chart.chartObj

        yield update({
          chartObj: chartObj.map(item=>item.id == action.index ?
          {...item, isLoading: false} : item)
        })
      }
    },
    *changeDateTime(action, {update, put, call}){

      let chartObjFromAction = action.chartObj

      let chartObj = chartObjFromAction.map(item=>item.needDatetime == true ? {
        ...item,
        isLoading: true,
        needDateState: false,
        scales: [],
        pieScales: [],
        scaleContainer: []
      } : item)

      yield update({chartObj: chartObj})

      for (let i = 0; i < chartObj.length; i++) {
        if (chartObj[i].needDatetime == true) {
          let sql = parseSql(chartObj[i].sql.replace(/#DATE#/g, action.dateTime))
          yield put({
            type: 'chart/fetchSql',
            sql: sql,
            index: chartObj[i].id,
            chartObj: chartObj
          })
        }
      }

    }
  }
}

export default chart